<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/10/15
 * Time: 9:46
 */

namespace pay\libs\alipay;




use pay\modules\frontend\models\Configuration;
use yii\base\Exception;
use pay\libs\Tools;
class AliPayApi extends AopClient
{

    private $pid;
    private $alipayAccount;
    private $reqUrl;
    private $value;
    private $charset = 'utf8';
    private $app_notify_url = 'http://pay.raysfly.com/frontend/pay-callback/call-ali-back-url';
//    private $app_notify_url = 'http://t.pay.raysfly.com/frontend/pay-callback/call-ali-back-url';


    public function __construct($channelMerId)
    {
        $configModel = new Configuration();
        $configData = $configModel->Configuration($channelMerId);
        if (!empty($configData)){
            $this->appId = $configData['appId'];
            $this->pid = $configData['pid'];
            $this->alipayAccount = $configData['alipayAccount'];
            $this->rsaPrivateKey = $configData['privateKey'];
            $this->alipayrsaPublicKey = $configData['alipayPublicKey'];
            $this->gatewayUrl = $configData['reqUrl'];
            $this->alipayPublicKey = $configData['alipayPublicKey'];

        }else{
            echo  Tools::showRes(99,'数据初始化失败，缺少配置信息');
            die;
        }

    }


    public function wapPay($data)
    {

        //请求参数
        $requestConfigs = array(
            'out_trade_no'=>$data['pay_order_sn'],
            'product_code'=>'QUICK_WAP_WAY',
            'total_amount'=>$data['amount'], //单位 元
            'subject'=>$data['subject'],  //订单标题
        );
        $commonConfigs = array(
            //公共参数
            'app_id' => $this->appId,
            'method' => 'alipay.trade.wap.pay',             //接口名称
            'format' => 'JSON',
            'return_url' => $data['returnUrl'],
            'charset'=> $this->charset,
            'sign_type'=>'RSA2',
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>'1.0',
            'notify_url' =>  $this->app_notify_url,
            'biz_content'=>json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        return $this->buildRequestForm($commonConfigs);

    }


    public function pagePay($data)
    {
        //请求参数
        $requestConfigs = array(
            'out_trade_no'=>$data['pay_order_sn'],
            'product_code'=>'FAST_INSTANT_TRADE_PAY',
            'total_amount'=>$data['amount'], //单位 元
            'subject'=>$data['subject'],  //订单标题
        );
        $commonConfigs = array(
            //公共参数
            'app_id' =>  $this->appId,
            'method' => 'alipay.trade.page.pay',             //接口名称
            'format' => 'JSON',
            'return_url' => $data['returnUrl'],
            'charset'=> $this->charset,
            'sign_type'=>'RSA2',
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>'1.0',
            'notify_url' => $this->app_notify_url,
            'biz_content'=>json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        return $this->buildRequestForm($commonConfigs);
    }
    
    
    
    public function refund($data)
    {
        //请求参数
        $requestConfigs = array(
            'out_trade_no'=>$data['paySerialId'],
            'refund_amount'=>100, //退款的金额 单位 元
            'refund_reason'=>$data['reason'],  // 退款的原因说明
        );
        $commonConfigs = array(
            //公共参数
            'app_id' =>  $this->appId,
            'method' => 'alipay.trade.refund',             //接口名称
            'format' => 'JSON',
            'return_url' => $data['returnUrl'],
            'charset'=> $this->charset,
            'sign_type'=>'RSA2',
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>'1.0',
            'notify_url' => $this->app_notify_url,
            'biz_content'=>json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
//        var_dump($commonConfigs);die;
        return $this->execute($commonConfigs);
    }




}